local desc_huanhua_2v2 = [[
  ## 幻化之战 2v2版本简介

  ---

  ## 身份说明

  游戏由两名忠臣和两名反贼进行，胜利目标为击杀所有敌人。

  座位排列可能是忠-反-反-忠或者忠-反-忠-反，以及对应的反贼在一号位的情况。

  ---

  ## 遗产

  当一名角色的队友死亡后，其执行〖幻化〗。

  ---

  ## 新生

  所有角色以自己的头像为武将，体力上限、体力值会被调整到4点、护甲会归零。

  ---

  ## 幻化

  〖幻化〗：从3个技能选择一个获得，或者摸两张牌。

  每轮开始时，存活角色各自执行〖幻化〗。

  ---

]]

local H = require "packages/activities/HuanhuaFunc"

--模式逻辑
local huanhua_2v2_getLogic = function()
  ---@class GameLogic
  local huanhua_2v2_logic = GameLogic:subclass("huanhua_2v2_logic")
  
  -- 身份详情
  function huanhua_2v2_logic:assignRoles()
    local room = self.room
    local roles = table.random {
      { "loyalist", "rebel", "rebel", "loyalist" },
      { "rebel", "loyalist", "loyalist", "rebel"},
    }

    for i = 1, 4 do
      local p = room.players[i]
      p.role = roles[i]
      p.role_shown = true
      room:broadcastProperty(p, "role")
    end

    room.players[1]:addBuddy(room.players[4])
    room.players[4]:addBuddy(room.players[1])
    room.players[2]:addBuddy(room.players[3])
    room.players[3]:addBuddy(room.players[2])

    self.start_role = roles[1]
    -- for adjustSeats
    room.players[1].role = "lord"
  end
  
  -- 皮套自备 - 修正版
  function huanhua_2v2_logic:chooseGenerals()
    local room = self.room
    local lord = room:getLord()
    room:setCurrent(lord)
    lord.role = self.start_role
    
    -- 首先设置所有玩家的身份显示
    for _, p in ipairs(room.players) do
      room:setPlayerProperty(p, "role_shown", true)
      room:broadcastProperty(p, "role")
    end

    -- 使用头像作为武将
    for _, p in ipairs(room.players) do
      local avatar = p._splayer:getAvatar()
      -- 确保头像有效
      if avatar == "anjiang" or avatar == "" or not Fk.generals[avatar] then
        avatar = "guanyu"  -- 默认使用关羽
      end
      
      local avatar_general = Fk.generals[avatar]
      if avatar_general then
        room:setPlayerGeneral(p, avatar_general.name, true)
        room:broadcastProperty(p, "general")
      else
        -- 如果头像对应的武将不存在，使用默认武将
        room:setPlayerGeneral(p, "guanyu", true)
        room:broadcastProperty(p, "general")
      end
      
      -- 设置体力属性
      room:setPlayerProperty(p, "shield", 0)
      room:setPlayerProperty(p, "maxHp", 4)
      room:setPlayerProperty(p, "hp", 4)
    end
    
    room:doBroadcastNotify("ShowToast", Fk:translate("huanhua_2v2_intro"))
    room:setTag("SkipNormalDeathProcess", true)
    
    -- 不要在这里创建技能池，应该在 GameStart 事件中创建
    -- H.createSkillPool(room)
  end
  
  -- 体力初始化（上面已经做了）
  function huanhua_2v2_logic:broadcastGeneral()
    -- 空实现，因为已经在chooseGenerals中处理了
  end
  
  -- 技能初始化
  function huanhua_2v2_logic:attachSkillToPlayers()
    -- 空实现，因为幻化模式不需要初始技能
  end

  return huanhua_2v2_logic
end

--游戏模式
local huanhua_2v2_mode = fk.CreateGameMode{
  name = "huanhua_2v2_mode",
  minPlayer = 4,
  maxPlayer = 4,
  rule = "#huanhua_2v2_rule&", --[[@as TriggerSkill]]
  logic = huanhua_2v2_getLogic,
  surrender_func = function(self, playedTime)
    local surrenderJudge = { 
      { text = "time limitation: 2 min", passed = playedTime >= 120 },
      { text = "2v2: left you alive", passed = table.find(Fk:currentRoom().players, function(p)
        return p.role == Self.role and p.dead and p.rest == 0
      end) and true } 
    }
    return surrenderJudge
  end,
  winner_getter = function(self, victim)
    if not victim.surrendered and victim.rest > 0 then
      return ""
    end
    local room = victim.room
    local alive = table.filter(room.players, function(p)
      return not p.surrendered and not (p.dead and p.rest == 0)
    end)
    if #alive == 0 then return "" end
    
    local winner = alive[1].role
    for _, p in ipairs(alive) do
      if p.role ~= winner then
        return ""
      end
    end
    return winner
  end,
}

Fk:loadTranslationTable{
  ["huanhua_2v2_mode"] = "幻化2v2",
  [":huanhua_2v2_mode"] = desc_huanhua_2v2,
  ["huanhua_2v2_intro"] = "选用巧妙的技能组合，合力击败对手吧！",
  ["time limitation: 2 min"] = "游戏时长达到2分钟",
  ["2v2: left you alive"] = "你所处队伍仅剩你存活",
}

return huanhua_2v2_mode